import Vue from 'vue';
import wx from 'weixin-js-sdk';
import { getWxConfig, authInfo } from '@/api/api';
import { getLocalStorage } from '@/utils/local-storage';
import { type } from 'jquery';

// 打包一个方法，抛出去变成全局方法
export default function wxShare({ title, desc, timelineTitle, link, imgUrl }) {
    const { shareId } = getLocalStorage('shareId');
    const { userId } = getLocalStorage('userId');
    const { directorId } = getLocalStorage('directorId');
    const { userLevel } = getLocalStorage('userLevel');
    if (Number(userLevel) == 2) {
        if (link == null) {
            link = process.env.VUE_APP_BASE_API + 'wechat/';
        } else {
            let param;
            param = '?userId=' + userId + '&shareId=' + userId;

            if (link.indexOf("?") != -1) {
                let num = link.indexOf("&userId");
                let numd = link.indexOf("?userId");
                if (num > 0) {
                    link = link.substr(0, num); param = '&userId=' + userId + '&shareId=' + userId;
                } else if (numd > 0) {
                    link = link.substr(0, numd);
                    param = '?userId=' + userId + '&shareId=' + userId;
                } else if (numd < 0 && num < 0) {
                    param = '&userId=' + userId + '&shareId=' + userId;
                }
            }

            let url = process.env.VUE_APP_BASE_API + 'wechat/#' + link + param;

            link = process.env.VUE_APP_BASE_API + 'wechat/static/html/redirect.html?app3Redirect=' + encodeURIComponent(url); // 分享链接
        }
    } else {
        if (directorId != null || directorId != undefined) {
            if (link == null) {
                link = process.env.VUE_APP_BASE_API + 'wechat/';
            } else {
                let param;
                param = '?userId=' + directorId + '&shareId=' + userId;



                if (link.indexOf("?") != -1) {
                    let num = link.indexOf("&userId");
                    let numd = link.indexOf("?userId");
                    if (num > 0) {
                        link = link.substr(0, num); param = '&userId=' + directorId + '&shareId=' + userId;
                    } else if (numd > 0) {

                        link = link.substr(0, numd);
                        param = '?userId=' + directorId + '&shareId=' + userId;
                    } else if (numd < 0 && num < 0) {
                        param = '&userId=' + directorId + '&shareId=' + userId;
                    }
                }

                let url = process.env.VUE_APP_BASE_API + 'wechat/#' + link + param;

                link = process.env.VUE_APP_BASE_API + 'wechat/static/html/redirect.html?app3Redirect=' + encodeURIComponent(url); // 分享链接
            }
        } else {
            if (link == null) {
                link = process.env.VUE_APP_BASE_API + 'wechat/';
            } else {
                let param;
                param = '?userId=' + directorId + '&shareId=' + userId;



                if (link.indexOf("?") != -1) {
                    let num = link.indexOf("&userId");
                    let numd = link.indexOf("?userId");
                    if (num > 0) {
                        link = link.substr(0, num); param = '&userId=' + directorId + '&shareId=' + userId;
                    } else if (numd > 0) {

                        link = link.substr(0, numd);
                        param = '?userId=' + directorId + '&shareId=' + userId;
                    } else if (numd < 0 && num < 0) {
                        param = '&userId=' + directorId + '&shareId=' + userId;
                    }

                }

                let url = process.env.VUE_APP_BASE_API + 'wechat/#' + link + param;

                link = process.env.VUE_APP_BASE_API + 'wechat/static/html/redirect.html?app3Redirect=' + encodeURIComponent(url); // 分享链接
            }
        }

    }



    getWxConfig({ url: process.env.VUE_APP_BASE_API + 'wechat/' }).then(res => {
        wx.config({
            // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            debug: false,
            // 必填，公众号的唯一标识
            appId: res.data.data.appId,
            // 必填，生成签名的时间戳
            timestamp: res.data.data.timestamp,
            // 必填，生成签名的随机串
            nonceStr: res.data.data.nonceStr,
            // 必填，签名，见附录1
            signature: res.data.data.signature,
            // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            jsApiList: [
                'checkJsApi',
                'updateAppMessageShareData',
                'updateTimelineShareData',
                'hideMenuItems',
                'openLocation',//打开位置
                'getLocation'//获得位置
            ]
        });
    });

    wx.error(function (res) {
        //console.log('出错了：' + res.errMsg); //这个地方的好处就是wx.config配置错误，会弹出窗口哪里错误，然后根据微信文档查询即可。
    });
    wx.ready(function () {
        wx.updateAppMessageShareData({
            title: title || '喷喷涂涂',
            desc: desc || title || '要喷涂，上喷喷涂涂', // 分享描述
            link: link || window.location.href, // 分享链接
            imgUrl: imgUrl || 'https://xintu361.oss-cn-qingdao.aliyuncs.com/sbhvjsrmjusefo547b0b.jpg' // 分享图标
        });

        wx.updateTimelineShareData({
            title: timelineTitle || desc || '要喷涂，上喷喷涂涂',
            link: link || window.location.href, // 分享链接
            imgUrl: imgUrl || 'https://xintu361.oss-cn-qingdao.aliyuncs.com/sbhvjsrmjusefo547b0b.jpg' // 分享图标
        });

        wx.hideMenuItems({
            menuList: ["menuItem:editTag", "menuItem:delete", "menuItem:originPage", "menuItem:readMode", "menuItem:openWithQQBrowser", "menuItem:openWithSafari", "menuItem:share:email", "menuItem:share:brand"] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮
        });
    });
}

// 为Vue的原型对象添加该方法，则所有vue实例都能继承该方法
Vue.prototype.$wxShare = wxShare
